package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.*;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.ConsultantService;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.util.Deductive;
import com.tt.fashionWebSite.util.ISSConstants;
import com.tt.fashionWebSite.util.Rule;
import com.tt.fashionWebSite.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by isoft-work 5 on 12/28/2014.
 */
@Service("consultantService")
public class ConsultantServiceImp extends GenericService implements ConsultantService {
   @Autowired
   @Qualifier("thongTinTuVan")
   private ThongTinTuVanDao thongTinTuVanDao;

   @Autowired
   @Qualifier("tapLuat")
   private TapLuatDao tapLuatDao;

   @Autowired
   @Qualifier("sanPhamFull")
   private SanPhamFullDao sanPhamFullDao;

   @Autowired
   @Qualifier("sizeAoTuVan")
   private SizeAoTuVanDao sizeAoTuVanDao;

   @Autowired
   @Qualifier("sizeQuanTuVan")
   private SizeQuanTuVanDao sizeQuanTuVanDao;

   @Autowired
   @Qualifier("ketQuaTuVan")
   private KetQuaTuVanDao ketQuaTuVanDao;

   @Autowired
   @Qualifier("soDoCoThe")
   private SoDoCoTheDao soDoCoTheDao;

   @Autowired
   @Qualifier("thongTinThanhVien")
   private ThongTinThanhVienDao thongTinThanhVienDao;

   @Autowired
   @Qualifier("hoaDonFull")
   private HoaDonFullDao hoaDonFullDao;

   @Autowired
   @Qualifier("loaiDanhMucFull")
   private LoaiDanhMucFullDao loaiDanhMucFullDao;

   @Override
   public ThongTinTuVanInfoDto getSupposed() {
      List<ThongTinTuVanDto> thongTinTuVans = mapper.mapCollection(thongTinTuVanDao.getSupposed(), ThongTinTuVanDto.class, THONG_TIN_TU_VAN);
      List<LoaiDanhMucFullDto> loaiDanhMucFullDtos = mapper.mapCollection(loaiDanhMucFullDao.getListMenuSuppose(), LoaiDanhMucFullDto.class);

      ThongTinTuVanInfoDto thongTinTuVanInfoDto = new ThongTinTuVanInfoDto();
      thongTinTuVanInfoDto.setLoaiDanhMucFulls(loaiDanhMucFullDtos);
      thongTinTuVanInfoDto.setThongTinTuVans(thongTinTuVans);

      return thongTinTuVanInfoDto;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public ConsultantDto consultantClothes(KetQuaTuVanDto ketQuaTuVanDto) {
//      run consultant
      ConsultantDto consultant = new ConsultantDto();
      List<TapLuat> tapLuats = tapLuatDao.getAll();
      List<List<String>> r = new ArrayList<List<String>>();
      List<List<String>> l = new ArrayList<List<String>>();
      List<String> gt = new ArrayList<String>();
      List<String> kl = new ArrayList<String>();
      // get list left and right
      if(tapLuats != null && tapLuats.size() > 0) {
         for(int i = 0; i < tapLuats.size(); i++) {
            l.add(StringUtils.getLeftAndGt(tapLuats.get(i).getVeTrai()));

            List<String> tem = new ArrayList<String>();
            tem.add(tapLuats.get(i).getVePhai());
            r.add(tem);
         }
      }
      gt = StringUtils.getLeftAndGt(ketQuaTuVanDto.getTapGiaThiet());
      Deductive deductive = new Deductive(l, r, gt);
      kl = filterConsultant(deductive.getGT());
      //      update info consultant
      KetQuaTuVan ketQuaTuVan = ketQuaTuVanDao.getInfoConsultantByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan());
      if(ketQuaTuVan == null) {
         ketQuaTuVan = new KetQuaTuVan();
         String st = kl.get(0);
         for(int k = 1; k < kl.size(); k++) {
            st += ("^" + kl.get(k));
         }

         SoDoCoThe soDoCoThe = soDoCoTheDao.getMemberMeasurementByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan());
         if(soDoCoThe == null) {
            soDoCoThe = new SoDoCoThe();
            Date now = new Date();
            soDoCoThe.setMaSDCoThe("SDCT" + now.getTime());
            soDoCoThe.setSdCanNang(ketQuaTuVanDto.getSoDoCoThe().getSdCanNang());
            soDoCoThe.setSdChieuCao(ketQuaTuVanDto.getSoDoCoThe().getSdChieuCao());
            soDoCoThe.setSdGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
            ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV());
            soDoCoThe.setThongTinThanhVien(thongTinThanhVien);
            soDoCoTheDao.insert(soDoCoThe);

            ketQuaTuVan.setSoDoCoThe(soDoCoThe);
         }
         else {
            soDoCoThe.setSdCanNang(ketQuaTuVanDto.getSoDoCoThe().getSdCanNang());
            soDoCoThe.setSdChieuCao(ketQuaTuVanDto.getSoDoCoThe().getSdChieuCao());
            soDoCoTheDao.update(soDoCoThe);
         }

         ketQuaTuVan.setSoDoCoThe(soDoCoThe);
         ketQuaTuVan.setTapGiaThiet(ketQuaTuVanDto.getTapGiaThiet());
         ketQuaTuVan.setTapKetLuan(st);
         ketQuaTuVan.setGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         ketQuaTuVanDao.insert(ketQuaTuVan);
      }
      else {
         String st = kl.get(0);
         for(int k = 1; k < kl.size(); k++) {
            st += ("^" + kl.get(k));
         }

         SoDoCoThe soDoCoThe = ketQuaTuVan.getSoDoCoThe();

         soDoCoThe.setSdCanNang(ketQuaTuVanDto.getSoDoCoThe().getSdCanNang());
         soDoCoThe.setSdChieuCao(ketQuaTuVanDto.getSoDoCoThe().getSdChieuCao());
         soDoCoTheDao.update(soDoCoThe);

         ketQuaTuVan.setTapGiaThiet(ketQuaTuVanDto.getTapGiaThiet());
         ketQuaTuVan.setTapKetLuan(st);
         ketQuaTuVanDao.update(ketQuaTuVan);
      }

//      get size tu van
        ConsultantSizeDto consultantSizeDto = consultantSizeClothes(ketQuaTuVanDto);

//      get product from result consultant
      List<SanPhamFullDto> sanPhamFullDtos = getProductConsultant(kl, ketQuaTuVanDto.getGioiTinhTuVan(), ketQuaTuVanDto.getDanhMucSP());
//      filter size
      if(ketQuaTuVanDto.getTypeSize().equals(ISSConstants.AO_SO_MI)) {
         sanPhamFullDtos = filterSizeConsultantClose(sanPhamFullDtos, consultantSizeDto.getSizeAoSoMi().get(0));
      }
      if(ketQuaTuVanDto.getTypeSize().equals(ISSConstants.AO_THUN)) {
         sanPhamFullDtos = filterSizeConsultantClose(sanPhamFullDtos, consultantSizeDto.getSizeAoThun().get(0));
      }
      if(ketQuaTuVanDto.getTypeSize().equals(ISSConstants.QUAN)) {
         sanPhamFullDtos = filterSizeConsultantClose(sanPhamFullDtos, consultantSizeDto.getSizeQuan().get(0));
      }
      consultant.setSanPhams(sanPhamFullDtos);
      return consultant;
   }

   private List<SanPhamFullDto> filterSizeConsultantClose(List<SanPhamFullDto> sanPhamFullDtos, String size) {
      if(sanPhamFullDtos != null && sanPhamFullDtos.size() > 0) {
         for(int i = 0; i < sanPhamFullDtos.size(); i++) {
            List<KichThuocDto> kichThuocDtos = sanPhamFullDtos.get(i).getSize();
            for(int j = 0; j < kichThuocDtos.size(); j++) {
               if (kichThuocDtos.get(j).getTenSize().equals(size)) {
                  break;
               }
               else {
                  if(j == kichThuocDtos.size() - 1) {
                     sanPhamFullDtos.remove(i);
                     i -= 1;
                  }
               }
            }
         }
      }

      return sanPhamFullDtos;
   }

   private List<SanPhamFullDto> getProductConsultant(List<String> consultant, String gender, String maDMSP) {
      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
      sanPhamFullDtos = sanPhamFullDao.getProductByIdDMSP(gender, maDMSP);

      // filter hoa tiet and mau sac
      if(sanPhamFullDtos != null && sanPhamFullDtos.size() > 0) {
         for(int i = 0; i < sanPhamFullDtos.size(); i++) {
            List<ChiTietSanPhamFullDto> chiTietSanPhamFulls = sanPhamFullDtos.get(i).getChiTietSanPhams();
            if(chiTietSanPhamFulls != null && chiTietSanPhamFulls.size() > 0) {
               for(int j = 0; j < chiTietSanPhamFulls.size(); j++) {
                  boolean br = false;
                  HoaTietDto hoaTietDto = chiTietSanPhamFulls.get(j).getHoaTiet();
                  for(int c = 0; c < consultant.size(); c++) {
                     if(!hoaTietDto.getMaHoaTiet().equals(consultant.get(c))) {
                        if(c == consultant.size() - 1) {
                           br = true;
                           sanPhamFullDtos.remove(i);
                           i -= 1;
                        }
                     }
                     else {
                        // check color
                        if(checkCorlor(chiTietSanPhamFulls.get(j).getMauSac(), consultant)){
                           br = true;
                           break;
                        }
                     }
                  }
                  if(br) {
                     break;
                  }
               }
            }
         }
      }

      sanPhamFullDtos = finalLstProduct(sanPhamFullDtos);
      return sanPhamFullDtos;
   }

   private boolean checkCorlor(MauSacDto mauSacDtos, List<String>  consultant) {
      boolean result = false;
      for(String c : consultant) {
         if(c.equals(mauSacDtos.getMaMau())) {
            return result = true;
         }
      }

      return result;
   }

   private List<SanPhamFullDto> finalLstProduct(List<SanPhamFullDto> sanPhamFullDtos) {
      for(SanPhamFullDto temSanPham : sanPhamFullDtos) {
         List<KichThuocDto> size = new ArrayList<KichThuocDto>();
         List<MauSacDto> color = new ArrayList<MauSacDto>();
         List<ChiTietSanPhamFullDto> temChiTietSPs = temSanPham.getChiTietSanPhams();
         for(ChiTietSanPhamFullDto temCT : temChiTietSPs) {
            size.add(temCT.getKichThuoc());
            color.add(temCT.getMauSac());
         }
         temSanPham.setSize(deleteLikeSize(size));
         temSanPham.setMauSac(deleteLikeColor(color));
      }
      return sanPhamFullDtos;
   }

   private List<KichThuocDto> deleteLikeSize(List<KichThuocDto> listSize) {
      List<KichThuocDto> result = listSize;
      if(listSize == null) {
         return null;
      }
      else {
         if (listSize.size() == 1 ){
            return result;
         }
         else {
            for(int j = 0; j < listSize.size() - 1; j++) {
               for(int i = j + 1; i < listSize.size(); i++) {
                  if(listSize.get(i).getMaSize().equals(listSize.get(j).getMaSize())) {
                     listSize.remove(i);
                     result = deleteLikeSize(listSize);
                  }
               }
            }
            return result;
         }
      }
   }

   private List<MauSacDto> deleteLikeColor(List<MauSacDto> listColor) {
      List<MauSacDto> result = listColor;
      if(listColor == null) {
         return null;
      }
      else {
         if (listColor.size() == 1 ){
            return result;
         }
         else {
            for(int j = 0; j < listColor.size() - 1; j++) {
               for(int i = j + 1; i < listColor.size(); i++) {
                  if(listColor.get(i).getMaMau().equals(listColor.get(j).getMaMau())) {
                     listColor.remove(i);
                     result = deleteLikeColor(listColor);
                  }
               }
            }
            return result;
         }
      }
   }

   private List<String> filterConsultant(List<String> kl) {
      List<ThongTinTuVan> lstGT = thongTinTuVanDao.getSupposed();
      if(lstGT != null && lstGT.size() > 0) {
         for(int i = 0; i < kl.size(); i++) {
            for(int j = 0; j < lstGT.size(); j++) {
               if(kl.get(i).equals(lstGT.get(j).getMaTT())) {
                  kl.remove(i);
                  i -= 1;
                  break;
               }
            }
         }
      }
      return kl;
   }

   @Override
   public ConsultantDto getConsultantByUser(String maTV, String gender) {
      ConsultantDto consultantDto = new ConsultantDto();
      KetQuaTuVan ketQuaTuVan = ketQuaTuVanDao.getInfoConsultantByUser(maTV, gender);
      if(ketQuaTuVan != null) {
         KetQuaTuVanDto ketQuaTuVanDto = mapper.map(ketQuaTuVan, KetQuaTuVanDto.class, KET_QUA_TU_VAN);
         consultantDto.setSoDoCoThe(ketQuaTuVanDto.getSoDoCoThe());
         List<String> gt = StringUtils.getLeftAndGt(ketQuaTuVanDto.getTapGiaThiet());
         consultantDto.setMauDa(gt.get(0));

         return consultantDto;
      }
      return null;
   }

   private ConsultantSizeDto consultantSizeClothes(KetQuaTuVanDto ketQuaTuVanDto) {
      ConsultantSizeDto consultantSizeDto = new ConsultantSizeDto();
      consultantSizeDto = getConsultantSizeClothes(ketQuaTuVanDto);
//      update so_do_co_the
      SoDoCoThe soDoCoThe = soDoCoTheDao.getMemberMeasurementByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan());
      if(soDoCoThe == null) {
         soDoCoThe = new SoDoCoThe();
         Date now = new Date();
         soDoCoThe.setMaSDCoThe("SDCT" + now.getTime());

         soDoCoThe.setSdDaiChan(ketQuaTuVanDto.getSoDoCoThe().getSdDaiChan());
         soDoCoThe.setSdDaiLung(ketQuaTuVanDto.getSoDoCoThe().getSdDaiLung());
         soDoCoThe.setSdDaiTay(ketQuaTuVanDto.getSoDoCoThe().getSdDaiTay());
         soDoCoThe.setSdDui(ketQuaTuVanDto.getSoDoCoThe().getSdDui());
         soDoCoThe.setSdMong(ketQuaTuVanDto.getSoDoCoThe().getSdMong());
         soDoCoThe.setSdNguc(ketQuaTuVanDto.getSoDoCoThe().getSdNguc());
         soDoCoThe.setSdEo(ketQuaTuVanDto.getSoDoCoThe().getSdEo());
         soDoCoThe.setSdVai(ketQuaTuVanDto.getSoDoCoThe().getSdVai());

         soDoCoThe.setSdGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV());
         soDoCoThe.setThongTinThanhVien(thongTinThanhVien);
         soDoCoTheDao.insert(soDoCoThe);
      }
      else {
         soDoCoThe.setSdDaiChan(ketQuaTuVanDto.getSoDoCoThe().getSdDaiChan());
         soDoCoThe.setSdDaiLung(ketQuaTuVanDto.getSoDoCoThe().getSdDaiLung());
         soDoCoThe.setSdDaiTay(ketQuaTuVanDto.getSoDoCoThe().getSdDaiTay());
         soDoCoThe.setSdDui(ketQuaTuVanDto.getSoDoCoThe().getSdDui());
         soDoCoThe.setSdMong(ketQuaTuVanDto.getSoDoCoThe().getSdMong());
         soDoCoThe.setSdNguc(ketQuaTuVanDto.getSoDoCoThe().getSdNguc());
         soDoCoThe.setSdEo(ketQuaTuVanDto.getSoDoCoThe().getSdEo());
         soDoCoThe.setSdVai(ketQuaTuVanDto.getSoDoCoThe().getSdVai());
         soDoCoTheDao.update(soDoCoThe);
      }

      updateConsultantSize(ketQuaTuVanDto, consultantSizeDto);

      SoDoCoTheDto soDoCoTheDto = mapper.map(soDoCoThe, SoDoCoTheDto.class, SO_DO_CO_THE_MAP_ID);
      consultantSizeDto.setSoDoCoThe(soDoCoTheDto);
      return consultantSizeDto;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED)
   public ConsultantSizeDto consultantSize(KetQuaTuVanDto ketQuaTuVanDto) {
      ConsultantSizeDto consultantSizeDto = new ConsultantSizeDto();
      consultantSizeDto = getConsultantSize(ketQuaTuVanDto);
//      update so_do_co_the
      SoDoCoThe soDoCoThe = soDoCoTheDao.getMemberMeasurementByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan());
      if(soDoCoThe == null) {
         soDoCoThe = new SoDoCoThe();
         Date now = new Date();
         soDoCoThe.setMaSDCoThe("SDCT" + now.getTime());

         soDoCoThe.setSdDaiChan(ketQuaTuVanDto.getSoDoCoThe().getSdDaiChan());
         soDoCoThe.setSdDaiLung(ketQuaTuVanDto.getSoDoCoThe().getSdDaiLung());
         soDoCoThe.setSdDaiTay(ketQuaTuVanDto.getSoDoCoThe().getSdDaiTay());
         soDoCoThe.setSdDui(ketQuaTuVanDto.getSoDoCoThe().getSdDui());
         soDoCoThe.setSdMong(ketQuaTuVanDto.getSoDoCoThe().getSdMong());
         soDoCoThe.setSdNguc(ketQuaTuVanDto.getSoDoCoThe().getSdNguc());
         soDoCoThe.setSdEo(ketQuaTuVanDto.getSoDoCoThe().getSdEo());
         soDoCoThe.setSdVai(ketQuaTuVanDto.getSoDoCoThe().getSdVai());

         soDoCoThe.setSdGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV());
         soDoCoThe.setThongTinThanhVien(thongTinThanhVien);
         soDoCoTheDao.insert(soDoCoThe);
      }
      else {
         soDoCoThe.setSdDaiChan(ketQuaTuVanDto.getSoDoCoThe().getSdDaiChan());
         soDoCoThe.setSdDaiLung(ketQuaTuVanDto.getSoDoCoThe().getSdDaiLung());
         soDoCoThe.setSdDaiTay(ketQuaTuVanDto.getSoDoCoThe().getSdDaiTay());
         soDoCoThe.setSdDui(ketQuaTuVanDto.getSoDoCoThe().getSdDui());
         soDoCoThe.setSdMong(ketQuaTuVanDto.getSoDoCoThe().getSdMong());
         soDoCoThe.setSdNguc(ketQuaTuVanDto.getSoDoCoThe().getSdNguc());
         soDoCoThe.setSdEo(ketQuaTuVanDto.getSoDoCoThe().getSdEo());
         soDoCoThe.setSdVai(ketQuaTuVanDto.getSoDoCoThe().getSdVai());
         soDoCoTheDao.update(soDoCoThe);
      }

      updateConsultantSize(ketQuaTuVanDto, consultantSizeDto);

      SoDoCoTheDto soDoCoTheDto = mapper.map(soDoCoThe, SoDoCoTheDto.class, SO_DO_CO_THE_MAP_ID);
      consultantSizeDto.setSoDoCoThe(soDoCoTheDto);
      return consultantSizeDto;
   }

   private void updateConsultantSize(KetQuaTuVanDto ketQuaTuVanDto, ConsultantSizeDto consultantSizeDto) {
//      quan
      SizeQuanTuVan sizeQuanTuVan = sizeQuanTuVanDao.getInfoByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan());
      ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV());
      if(sizeQuanTuVan == null) {
         sizeQuanTuVan = new SizeQuanTuVan();
         sizeQuanTuVan.setGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         sizeQuanTuVan.setSizeQuan(consultantSizeDto.getSizeQuan().get(0));
         sizeQuanTuVan.setThongTinThanhVien(thongTinThanhVien);

         sizeQuanTuVanDao.insert(sizeQuanTuVan);
      }
      else {
         sizeQuanTuVan.setSizeQuan(consultantSizeDto.getSizeQuan().get(0));
         sizeQuanTuVanDao.update(sizeQuanTuVan);
      }

//      ao so mi
      SizeAoTuVan sizeAoTuVanSM = sizeAoTuVanDao.getInfoByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan(), ISSConstants.TYPE_SIZE_SM);
      if(sizeAoTuVanSM == null) {
         sizeAoTuVanSM = new SizeAoTuVan();
         sizeAoTuVanSM.setGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         sizeAoTuVanSM.setLoaiTrangPhuc(ISSConstants.TYPE_SIZE_SM);
         sizeAoTuVanSM.setSizeAo(consultantSizeDto.getSizeAoSoMi().get(0));
         sizeAoTuVanSM.setThongTinThanhVien(thongTinThanhVien);

         sizeAoTuVanDao.insert(sizeAoTuVanSM);
      }
      else {
         sizeAoTuVanSM.setSizeAo(consultantSizeDto.getSizeAoSoMi().get(0));

         sizeAoTuVanDao.update(sizeAoTuVanSM);
      }

//      ao thun
      SizeAoTuVan sizeAoTuVanT = sizeAoTuVanDao.getInfoByUser(ketQuaTuVanDto.getSoDoCoThe().getThongTinThanhVien().getMaTV(), ketQuaTuVanDto.getGioiTinhTuVan(), ISSConstants.TYPE_SIZE_T);
      if(sizeAoTuVanT == null) {
         sizeAoTuVanT = new SizeAoTuVan();
         sizeAoTuVanT.setGioiTinhTuVan(ketQuaTuVanDto.getGioiTinhTuVan());
         sizeAoTuVanT.setLoaiTrangPhuc(ISSConstants.TYPE_SIZE_T);
         sizeAoTuVanT.setSizeAo(consultantSizeDto.getSizeAoThun().get(0));
         sizeAoTuVanT.setThongTinThanhVien(thongTinThanhVien);

         sizeAoTuVanDao.insert(sizeAoTuVanT);
      }
      else {
         sizeAoTuVanT.setSizeAo(consultantSizeDto.getSizeAoThun().get(0));

         sizeAoTuVanDao.update(sizeAoTuVanT);
      }

   }

   private ConsultantSizeDto getConsultantSizeClothes(KetQuaTuVanDto ketQuaTuVanDto) {
      ConsultantSizeDto consultantSizeDto = new ConsultantSizeDto();
      ConsultantSizeValueDto consultantSizeValueDto = new ConsultantSizeValueDto();
      SoDoCoTheDto soDoCoTheDto = ketQuaTuVanDto.getSoDoCoThe();

      if(ketQuaTuVanDto.getGioiTinhTuVan().equals("NAM")) {
         // Ao so mi (vai, dai tay, dai lung, nguc, mong)
         List<Long> numberSizeSoMi = new ArrayList<Long>();
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiSoMiNam()));
//         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiTay(), consultantSizeValueDto.getDaiTaySoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiLungSoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucSoMiNam()));
//         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongSoMiNam()));
         consultantSizeDto.setSizeAoSoMi(getStringSize(numberSizeSoMi, consultantSizeValueDto.getKichThuocAoSoMiQuanNam()));

         // quan (eo, mong, dui)
         List<Long> numberSizeQuan = new ArrayList<Long>();
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoQuanNam()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongQuanNam()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdDui(), consultantSizeValueDto.getDuiQuanMiNam()));
         consultantSizeDto.setSizeQuan(getStringSize(numberSizeQuan, consultantSizeValueDto.getKichThuocAoSoMiQuanNam()));

         // Ao thun (nguc, vai, dai)
         List<Long> numberSizeAoThun = new ArrayList<Long>();
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucThunNam()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiThunNam()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiThunNam()));
         consultantSizeDto.setSizeAoThun(getStringSize(numberSizeAoThun, consultantSizeValueDto.getKichThuocAoThunNam()));
      }
      else {
         // Ao so mi (vai, dai tay, dai lung, nguc, mong, eo)
         List<Long> numberSizeSoMi = new ArrayList<Long>();
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiSoMiNu()));
//         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiTay(), consultantSizeValueDto.getDaiTaySoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiLungSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucSoMiNu()));
//         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoSoMiNu()));
         consultantSizeDto.setSizeAoSoMi(getStringSize(numberSizeSoMi, consultantSizeValueDto.getKichThuocAoSoMiQuanNu()));

         // quan (eo, mong, dai)
         List<Long> numberSizeQuan = new ArrayList<Long>();
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoQuanNu()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongQuanNu()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdDaiChan(), consultantSizeValueDto.getDaiQuanMiNu()));
         consultantSizeDto.setSizeQuan(getStringSize(numberSizeQuan, consultantSizeValueDto.getKichThuocAoSoMiQuanNam()));

         // Ao thun (nguc, vai, dai)
         List<Long> numberSizeAoThun = new ArrayList<Long>();
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucThunNu()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiThunNu()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiThunNu()));
         consultantSizeDto.setSizeAoThun(getStringSize(numberSizeAoThun, consultantSizeValueDto.getKichThuocAoThunNu()));
      }
      return consultantSizeDto;
   }

   private ConsultantSizeDto getConsultantSize(KetQuaTuVanDto ketQuaTuVanDto) {
      ConsultantSizeDto consultantSizeDto = new ConsultantSizeDto();
      ConsultantSizeValueDto consultantSizeValueDto = new ConsultantSizeValueDto();
      SoDoCoTheDto soDoCoTheDto = ketQuaTuVanDto.getSoDoCoThe();

      if(ketQuaTuVanDto.getGioiTinhTuVan().equals("NAM")) {
         // Ao so mi (vai, dai tay, dai lung, nguc, mong)
         List<Long> numberSizeSoMi = new ArrayList<Long>();
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiSoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiTay(), consultantSizeValueDto.getDaiTaySoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiLungSoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucSoMiNam()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongSoMiNam()));
         consultantSizeDto.setSizeAoSoMi(getStringSize(numberSizeSoMi, consultantSizeValueDto.getKichThuocAoSoMiQuanNam()));

         // quan (eo, mong, dui)
         List<Long> numberSizeQuan = new ArrayList<Long>();
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoQuanNam()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongQuanNam()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdDui(), consultantSizeValueDto.getDuiQuanMiNam()));
         consultantSizeDto.setSizeQuan(getStringSize(numberSizeQuan, consultantSizeValueDto.getKichThuocAoSoMiQuanNam()));

         // Ao thun (nguc, vai, dai)
         List<Long> numberSizeAoThun = new ArrayList<Long>();
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucThunNam()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiThunNam()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiThunNam()));
         consultantSizeDto.setSizeAoThun(getStringSize(numberSizeAoThun, consultantSizeValueDto.getKichThuocAoThunNam()));
      }
      else {
         // Ao so mi (vai, dai tay, dai lung, nguc, mong, eo)
         List<Long> numberSizeSoMi = new ArrayList<Long>();
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiTay(), consultantSizeValueDto.getDaiTaySoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiLungSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongSoMiNu()));
         numberSizeSoMi.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoSoMiNu()));
         consultantSizeDto.setSizeAoSoMi(getStringSize(numberSizeSoMi, consultantSizeValueDto.getKichThuocAoSoMiQuanNu()));

         // quan (eo, mong, dai)
         List<Long> numberSizeQuan = new ArrayList<Long>();
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdEo(), consultantSizeValueDto.getEoQuanNu()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdMong(), consultantSizeValueDto.getMongQuanNu()));
         numberSizeQuan.add(getNumberSize(soDoCoTheDto.getSdDaiChan(), consultantSizeValueDto.getDaiQuanMiNu()));
         consultantSizeDto.setSizeQuan(getStringSize(numberSizeQuan, consultantSizeValueDto.getKichThuocAoSoMiQuanNu()));

         // Ao thun (nguc, vai, dai)
         List<Long> numberSizeAoThun = new ArrayList<Long>();
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdNguc(), consultantSizeValueDto.getNgucThunNu()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdVai(), consultantSizeValueDto.getVaiThunNu()));
         numberSizeAoThun.add(getNumberSize(soDoCoTheDto.getSdDaiLung(), consultantSizeValueDto.getDaiThunNu()));
         consultantSizeDto.setSizeAoThun(getStringSize(numberSizeAoThun, consultantSizeValueDto.getKichThuocAoThunNu()));
      }
      return consultantSizeDto;
   }

   private List<String> getStringSize(List<Long> number, List<String> list) {
      List<String> result = new ArrayList<String>();
      Integer tem = 0;
      for(int i = 0; i < number.size(); i++) {
         if(tem < number.get(i)) {
            tem = Integer.parseInt(number.get(i).toString());
         }
      }
      result.add(list.get(tem));
      return result;
   }

   private Long getNumberSize(Long size, List<Long> list) {
      Integer value = 0;
      if(size != null) {
         for(int i =  0; i < list.size(); i++) {
            if(size >= list.get(i)) {
               value = i + 1;
            }
         }
      }
      Long re = Long.parseLong(value.toString());
      return re;
   }

   @Override
   public ConsultantSizeDto consultantSizeByUser(String maTV, String gender) {
      ConsultantSizeDto consultantSizeDto = new ConsultantSizeDto();
      List<String> list = new ArrayList<String>();
      List<String> list1 = new ArrayList<String>();
      List<String> list2 = new ArrayList<String>();

      SizeAoTuVanDto sizeAoTuVanSM = mapper.map(sizeAoTuVanDao.getInfoByUser(maTV, gender, ISSConstants.TYPE_SIZE_SM), SizeAoTuVanDto.class, SIZE_AO_TU_VAN_MAP_ID);
      SizeAoTuVanDto sizeAoTuVanT = mapper.map(sizeAoTuVanDao.getInfoByUser(maTV, gender, ISSConstants.TYPE_SIZE_T), SizeAoTuVanDto.class, SIZE_AO_TU_VAN_MAP_ID);
      SizeQuanTuVanDto sizeQuanTuVanDto = mapper.map(sizeQuanTuVanDao.getInfoByUser(maTV, gender), SizeQuanTuVanDto.class, SIZE_QUAN_TU_VAN_MAP_ID);
      list.add(sizeAoTuVanSM.getSizeAo());
      list1.add(sizeAoTuVanT.getSizeAo());
      list2.add(sizeQuanTuVanDto.getSizeQuan());

      SoDoCoTheDto soDoCoTheDto = mapper.map(soDoCoTheDao.getMemberMeasurementByUser(maTV, gender), SoDoCoTheDto.class, SO_DO_CO_THE_MAP_ID);

      consultantSizeDto.setSoDoCoThe(soDoCoTheDto);
      consultantSizeDto.setSizeAoSoMi(list);
      consultantSizeDto.setSizeAoThun(list1);
      consultantSizeDto.setSizeQuan(list2);
      return consultantSizeDto;
   }
}
